var app = new Vue({
	el: '#app',
	data: {
		url: {
			getAutoMockDropDownDatas: '/api/AutoMock/GetAutoMockDropDownDatas', //获取保单类型
			getProductByID: '/api/Product/GetProductByID', //获取保单详细数据
			getMoney_Intelligent: '/api/appfun/GetMoney_Intelligent', //支付金额
			addAutoMock: '/api/AutoMock/AddAutoMock', //提交保单
			genOrder: "/api/Order/GenOrder", //获取订单号，支付
		},
		userinfo: globalData.userInfo, //用户信息
		product_detail: {}, //产品信息
		product_list: [], //保单列表
		form_data: {
			AutoMock: {
				ID: "",
				Name: "",
				Sex: 1, //默认男
				Birthday: "", // "2017-08-26",
				International: "",
				LiveAddress: "",
				IsSmoking: 'true',
				ProductName: "", //"储蓄分红A1", 			
				MoneyType: "", //"0ce927df9da349d49c33144a21fec790",
				PayYear: "", //"5年", 
				Money: "", //"1500", 
				IsDrawing: 'false',
				ProductID: globalData.getStorage('product_detail').ID, //"2ae78608-86c4-4fe5-9c09-11e4a709525b", 
				PolicyName: "", //"投保人",
				PolicySex: 1,
				PolicyBirthday: "", //"2018-08-25",
				PolicyInternational: "", //"美国",
				PolicyLiveAddress: "", //"洛杉矶", 
				PolicyIsSmoking: true,
				BeginYear: "", //"3", 
				DrawingMoney: "", //"3000",
				Remark: "", //"哈哈",
				UserConfirmEmail: "wrggg@q.com"
			},
			Order: {
				Money: "1500",
				TradeName: "储蓄分红A1",
				TradeNo: "95fca008-44c1-4ed0-8953-dd581cf8d2ac",
				TradeCount: "1",
				Remark: "哈哈",
				OrderType: "2",
				OrderNo: "5年"
			}
		},
		CurrencyType: [], //货币种类
		PayYear: [], //供款年限
		ProtectType: '', //年缴保费
		IsGetMoney: 0, //是否提款
		isSamePeople: 0, //是否为同一人
		email_show: false, //确认邮箱
		MyOwnCoins: 0, //我的金币
		SpendCoins: 0, //花费金币

	},
	computed: {

	},
	mounted: function() {
		console.log(globalData.getStorage('product_detail'))
		var that = this;
//		that.confirm_email()
		that.product_detail = globalData.getStorage('product_detail')
		//设置初始产品ID
		//		that.form_data.AutoMock.ProductID = that.product_detail.ID
		console.log(that.form_data.AutoMock.ProductID)
		globalData.getTempToken(function() {
			$(".loading").hide()
			that.form_data.AutoMock.UserConfirmEmail = that.userinfo.Email
			globalData.ajaxPost(that.url.getAutoMockDropDownDatas, { ID: that.product_detail.ID }, function(res) {
				console.log(res)
				if(res.suc) {
					that.product_list = res.data.ProductNames
					setTimeout(function() {
						$('#PolicyBirthday').datetimepicker({
							language: 'zh-CN',
							format: "yyyy-mm-dd",
							weekStart: 1,
							todayBtn: 1,
							autoclose: 1,
							todayHighlight: 1,
							startView: 2,
							minView: 2,
							forceParse: 0,
							pickerPosition: "bottom-left"
						});
						$('#Birthday').datetimepicker({
							language: 'zh-CN',
							format: "yyyy-mm-dd",
							weekStart: 1,
							todayBtn: 1,
							autoclose: 1,
							todayHighlight: 1,
							startView: 2,
							minView: 2,
							forceParse: 0,
							pickerPosition: "bottom-left"
						});
						console.log($('.form_date'))
						$('.form_date').bootstrapValidator(valid_json);
					}, 100)
				}
			});

		})
		//获取保单详细信息
		that.getProductByID(that.product_detail.ID)
	},
	methods: {
		getProductByID: function(id) {
			var that = this;
			globalData.ajaxPost(this.url.getAutoMockDropDownDatas, { ID: id }, function(res) {
				console.log(res)
				if(res.suc) {
					that.CurrencyType = res.data.CurrencyType
					that.PayYear = res.data.PayYear
					that.ProtectType = res.data.ProtectType
					that.IsGetMoney = res.data.IsGetMoney
					//赋值货币种类，缴费年限 
					that.form_data.AutoMock.MoneyType = res.data.CurrencyType[0].Key
					that.form_data.AutoMock.PayYear = res.data.PayYear[0].Key
					//初始化货币
					setTimeout(function(){
						that.select_current()
					},100)
					
				}
			});

		},
		change_product: function(id) {
			var that = this;
			that.getProductByID(id)
		},
		select_current:function(event){
//			console.log(event.target)
//			console.log($(event.target).children('option:selected').text())
			var text = $('.current_select option:selected').text()
			$(".current_type").text(text)
		},
		//提交
		Submit: function() {
			var that = this;
			console.log(that.userinfo)
			$('.form_date').bootstrapValidator('validate');
			var b = $('.form_date').data("bootstrapValidator").isValid();
			if(!b)
				return;
			//判断是否为同一人,出生时间
			if(!$("#Birthday").val()) {
				$("#alert").modal()
				$("#alert p").text('请选择受保人出生日期')
				return;
			}else if(that.isSamePeople != 1 && !$("#PolicyBirthday").val()){
				$("#alert").modal()
				$("#alert p").text('请选择保单持有者出生日期')
				return;
			}
			
			$("#confirm_email").modal()
		},
		confirm_email: function() {
			var that = this;
			//弹出支付页面
			globalData.ajaxPost(this.url.getMoney_Intelligent, null, function(res) {
				console.log(res)
				if(res.suc) {
					$("#confirm_pay").modal()
					$("#confirm_pay p").text('计划书需要' + res.data.SpendCoins + "个金币")
					$("#confirm_pay span").text('您的账户们目前还有' + res.data.MyOwnCoins + "个金币")
					that.SpendCoins = res.data.SpendCoins
					that.MyOwnCoins = res.data.MyOwnCoins
				} else {
					$("#alert").modal()
					$("#alert p").text(res.msg)
				}
			});
		},
		confirm_pay: function() {    //付款
			var that = this;
			if(that.SpendCoins > that.MyOwnCoins) {
				$("#alert").modal()
				$("#alert p").text('余额不足，请充值')
				return;
			}
			console.log(that.product_detail)
			var product_item = that.product_detail
			var data = {
				'CargoName': product_item.ProductName,
				'CargoNo': product_item.ID,
				'CargoCount': '1',
				'Money': that.SpendCoins,
				'Remark': '智能模拟支付|' + product_item.ID + '|',
				'OrderType': '2'
			}
			globalData.ajaxPost("/api/Order/GenOrder", data, function(res) {
				console.log(res)
				if(res.suc) {
					that.addAutoMock()
				} else {
					$("#alert").modal()
					$("#alert p").text(res.msg)
				}
			})
		},
		addAutoMock: function() {
			var that = this;
			that.form_data.AutoMock.Birthday = $("#Birthday").val()
			//判断是否是同一人
			console.log(that.isSamePeople)
			if(that.isSamePeople == 1){
				//投保人信息
				that.form_data.AutoMock.PolicyName = that.form_data.AutoMock.Name
				that.form_data.AutoMock.PolicySex = that.form_data.AutoMock.Sex				
				that.form_data.AutoMock.PolicyInternational = that.form_data.AutoMock.International
				that.form_data.AutoMock.PolicyLiveAddress = that.form_data.AutoMock.LiveAddress
				that.form_data.AutoMock.PolicyIsSmoking = that.form_data.AutoMock.IsSmoking
				that.form_data.AutoMock.PolicyBirthday = that.form_data.AutoMock.Birthday
			}else{
				that.form_data.AutoMock.PolicyBirthday = $("#PolicyBirthday").val()
			}
			//填写order信息
			that.form_data.Order.Money = that.form_data.AutoMock.Money
			that.form_data.Order.TradeName = that.product_detail.ProductName
			that.form_data.Order.TradeNo = that.product_detail.ID
			that.form_data.Order.Remark = that.form_data.AutoMock.Remark
			that.form_data.Order.OrderNo = that.form_data.AutoMock.PayYear
			console.log(that.form_data)
			//判断是否提款
			if(that.form_data.AutoMock.IsDrawing == 'false'){
				that.form_data.AutoMock.BeginYear = ''
				that.form_data.AutoMock.DrawingMoney = ''
			}
			console.log(that.form_data)
			globalData.ajaxPost(that.url.addAutoMock,that.form_data,function(res) {
				console.log(res)
				if(res.suc) {
					$("#more_apply").modal()
					$("#more_apply").click(function(){
						window.location.reload()
					})
				} else {
					$("#alert").modal()
					$("#alert p").text(res.msg)
				}

			})
			console.log(that.form_data)
		}
	}
})

var valid_json = {
	feedbackIcons: {
		valid: 'glyphicon glyphicon-ok',
		invalid: 'glyphicon glyphicon-remove',
		validating: 'glyphicon glyphicon-refresh'
	},
	fields: {
		Name: {
			validators: {
				notEmpty: {
					message: '姓名不能为空'
				}
			},
		},
		International: {
			validators: {
				notEmpty: {
					message: '国籍不能为空'
				}
			},
		},
		LiveAddress: {
			validators: {
				notEmpty: {
					message: '居住地不能为空'
				}
			},
		},
		Money: {
			validators: {
				notEmpty: {
					message: '缴费金额不能为空'
				}
			},
		},
		BeginYear: {
			validators: {
				notEmpty: {
					message: '开始年份不能为空'
				}
			},
		},
		DrawingMoney: {
			validators: {
				notEmpty: {
					message: '提取金额不能为空'
				}
			},
		},
		PolicyName: {
			validators: {
				notEmpty: {
					message: '姓名不能为空'
				}
			},
		},
		PolicyInternational: {
			validators: {
				notEmpty: {
					message: '国籍不能为空'
				}
			},
		},
		PolicyLiveAddress: {
			validators: {
				notEmpty: {
					message: '居住地不能为空'
				}
			},
		}
	}
}